package com.bonc.hbase.api.util;


import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 校验日期时间工具类
 * 
 * @author caiqiang
 * @version 2016年8月3日
 * @see DateVerificationUtil
 * @since
 */
public class DateVerificationUtil {
    /**
     * 日志对象
     */
    private static final Logger LOG = LoggerFactory.getLogger(DateVerificationUtil.class);

    /**
     * Description:验证小时输入的格式
     * 
     * @param hour
     * @return boolean
     * @see
     */
    public static boolean verificationHour(String hour) {

        Integer hourI = Integer.parseInt(hour);
        if (hourI >= 0 && hourI <= 24) {
            return true;
        }

        return false;
    }

    /**
     * Description:验证日期输入是否满足在同一月内
     * 
     * @param startDateStr
     * @param endDateStr
     * @param startHour
     * @param endHour
     * @return boolean
     * @see
     */
    public static boolean verificationDate(String startDateStr, String endDateStr,
                                           String startHour, String endHour) {

        if (!correctDate(startDateStr) && !correctDate(endDateStr)) {
            LOG.error("please input correct date");
            return false;
        }

        if (verificationHour(startHour) && verificationHour(endHour)) {
            return true;
        }
        return false;
    }

    /**
     * Description:验证日期输入的格式
     * 
     * @param date
     * @return boolean
     * @see
     */
    public static boolean correctDate(String date) {

        Pattern p = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\-\\s]?((((0?"
                                    + "[13578])|(1[02]))[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))"
                                    + "|(((0?[469])|(11))[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])|(30)))|"
                                    + "(0?2[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][12"
                                    + "35679])|([13579][01345789]))[\\-\\-\\s]?((((0?[13578])|(1[02]))"
                                    + "[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))"
                                    + "[\\-\\-\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\-\\s]?((0?["
                                    + "1-9])|(1[0-9])|(2[0-8]))))))");

        return p.matcher(date).matches();
    }

    private DateVerificationUtil() {}
}